package com.cloudeasy.post.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.EntityUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.post.ReplyVO;
import com.cloudeasy.api.vo.post.SimpleUserInfoVO;
import com.cloudeasy.post.field.ReplyField;
import com.cloudeasy.post.po.Reply;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评论工具类
 */
public class ReplyUtil {
    /**
     * 构建查询
     *
     * @param postId 帖子Id
     * @param type 类型
     * @param asc  正序排序字段
     * @param desc 倒序排序字段
     * @return {@link QueryWrapper<Reply>}
     */
    public static QueryWrapper<Reply> compose(String postId,  Integer type, String[] asc, String[] desc) {
        QueryWrapper<Reply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id", postId);
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        if (asc != null && asc.length > 0) {
            EntityUtil.checkParamter(ReplyField.getFieldList(), asc);
            queryWrapper.orderByAsc(asc);
        }
        if (desc != null && desc.length > 0) {
            EntityUtil.checkParamter(ReplyField.getFieldList(), desc);
            queryWrapper.orderByDesc(desc);
        }
        return queryWrapper;
    }

    /**
     * 转换VO对象
     *
     * @param reply               {@link Reply}
     * @param simpleUserInfoVOMap {@link Map<String, SimpleUserInfoVO>}
     * @return {@link ReplyVO}
     */
    public static ReplyVO convert(Reply reply, Map<String, SimpleUserInfoVO> simpleUserInfoVOMap) {
        ReplyVO replyVO = new ReplyVO();
        BeanUtils.copyProperties(reply, replyVO);
        replyVO.setReviewer(simpleUserInfoVOMap.get(reply.getReviewerId()));
        return replyVO;
    }

    /**
     * 转换VO对象
     *
     * @param replies           {@link List<Reply>}
     * @param simpleUserInfoVOS {@link List<SimpleUserInfoVO>}
     * @return {@link List<ReplyVO>}
     */
    public static List<ReplyVO> convert(List<Reply> replies, List<SimpleUserInfoVO> simpleUserInfoVOS) {
        Map<String, SimpleUserInfoVO> simpleUserInfoVOMap = toMap(simpleUserInfoVOS);
        return replies.stream().map(reply -> convert(reply, simpleUserInfoVOMap)).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param replyIPage        {@link List<Reply>}
     * @param simpleUserInfoVOS {@link List<SimpleUserInfoVO>}
     * @return {@link PageResult<ReplyVO>}
     */
    public static PageResult<ReplyVO> convert(IPage<Reply> replyIPage, List<SimpleUserInfoVO> simpleUserInfoVOS) {
        Map<String, SimpleUserInfoVO> simpleUserInfoVOMap = toMap(simpleUserInfoVOS);
        PageResult<ReplyVO> replyVOPageResult = new PageResult<>();
        replyVOPageResult.setTotal(replyIPage.getTotal())
                .setPages(replyIPage.getPages())
                .setRecords(replyIPage.getRecords().stream().map(reply -> convert(reply, simpleUserInfoVOMap)).collect(Collectors.toList()));
        return replyVOPageResult;
    }

    /**
     * 将简单用户信息列表转化为Id为key的Map
     *
     * @param simpleUserInfoVOS {@link List<SimpleUserInfoVO>}
     * @return {@link Map<String, SimpleUserInfoVO>}
     */
    private static Map<String, SimpleUserInfoVO> toMap(List<SimpleUserInfoVO> simpleUserInfoVOS) {
        return simpleUserInfoVOS.stream().collect(Collectors.toMap(SimpleUserInfoVO::getId, simpleUserInfoVO -> simpleUserInfoVO, (o, o2) -> o));
    }
}
